package o;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import androidx.annotation.NonNull;
import com.huawei.appgallery.common.media.api.OriginalMediaBean;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ThreadFactory;
import o.bad.e;

public class bae {
    private boolean ˊ = false;
    private List<String> ˋ = new ArrayList();
    private List<OriginalMediaBean> ˎ = null;
    private String ˏ = null;

    static class c implements ThreadFactory {
        private c() {
        }

        public Thread newThread(@NonNull Runnable runnable) {
            return new Thread(runnable, "TranslateImgHelper");
        }
    }

    static class d implements Callable<Boolean> {
        private String ˊ;
        private CompressFormat ˎ;
        private String ˏ;

        public /* synthetic */ Object call() throws Exception {
            return ॱ();
        }

        public d(String str, String str2, CompressFormat compressFormat) {
            this.ˏ = str;
            this.ˊ = str2;
            this.ˎ = compressFormat;
        }

        public Boolean ॱ() throws Exception {
            return Boolean.valueOf(ˏ());
        }

        public boolean ˏ() {
            Closeable fileOutputStream;
            Closeable bufferedOutputStream;
            Throwable e;
            Throwable th;
            Closeable closeable = null;
            boolean z = false;
            try {
                Bitmap ˊ = bad.ˋ().ˊ(new e(), this.ˏ);
                if (ˊ == null) {
                    dnw.ˎ(null);
                    dnw.ˎ(null);
                } else {
                    File file = new File(this.ˊ);
                    if (!file.createNewFile()) {
                        azh.ˏ.ˏ("TranslateImgHelper", "translateImgToWebp createNewFile false");
                    }
                    fileOutputStream = new FileOutputStream(file);
                    try {
                        bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                    } catch (IOException e2) {
                        e = e2;
                        bufferedOutputStream = fileOutputStream;
                        try {
                            azh.ˏ.ˊ("TranslateImgHelper", "translateImgToWebp, e: ", e);
                            dnw.ˎ(closeable);
                            dnw.ˎ(bufferedOutputStream);
                            return z;
                        } catch (Throwable th2) {
                            th = th2;
                            fileOutputStream = bufferedOutputStream;
                            dnw.ˎ(closeable);
                            dnw.ˎ(fileOutputStream);
                            throw th;
                        }
                    } catch (Exception e3) {
                        e = e3;
                        try {
                            azh.ˏ.ˊ("TranslateImgHelper", "translateImgToWebp, e: ", e);
                            dnw.ˎ(closeable);
                            dnw.ˎ(fileOutputStream);
                            return z;
                        } catch (Throwable th3) {
                            th = th3;
                            dnw.ˎ(closeable);
                            dnw.ˎ(fileOutputStream);
                            throw th;
                        }
                    }
                    try {
                        ˊ.compress(this.ˎ, 50, bufferedOutputStream);
                        bufferedOutputStream.flush();
                        z = true;
                        ˊ.recycle();
                        dnw.ˎ(bufferedOutputStream);
                        dnw.ˎ(fileOutputStream);
                    } catch (IOException e4) {
                        e = e4;
                        closeable = bufferedOutputStream;
                        bufferedOutputStream = fileOutputStream;
                        azh.ˏ.ˊ("TranslateImgHelper", "translateImgToWebp, e: ", e);
                        dnw.ˎ(closeable);
                        dnw.ˎ(bufferedOutputStream);
                        return z;
                    } catch (Exception e5) {
                        e = e5;
                        closeable = bufferedOutputStream;
                        azh.ˏ.ˊ("TranslateImgHelper", "translateImgToWebp, e: ", e);
                        dnw.ˎ(closeable);
                        dnw.ˎ(fileOutputStream);
                        return z;
                    } catch (Throwable th4) {
                        th = th4;
                        closeable = bufferedOutputStream;
                        dnw.ˎ(closeable);
                        dnw.ˎ(fileOutputStream);
                        throw th;
                    }
                }
            } catch (IOException e6) {
                e = e6;
                bufferedOutputStream = null;
                azh.ˏ.ˊ("TranslateImgHelper", "translateImgToWebp, e: ", e);
                dnw.ˎ(closeable);
                dnw.ˎ(bufferedOutputStream);
                return z;
            } catch (Exception e7) {
                e = e7;
                fileOutputStream = null;
                azh.ˏ.ˊ("TranslateImgHelper", "translateImgToWebp, e: ", e);
                dnw.ˎ(closeable);
                dnw.ˎ(fileOutputStream);
                return z;
            } catch (Throwable th5) {
                th = th5;
                fileOutputStream = null;
                dnw.ˎ(closeable);
                dnw.ˎ(fileOutputStream);
                throw th;
            }
            return z;
        }
    }

    public bae(List<OriginalMediaBean> list, String str, boolean z) {
        this.ˎ = list;
        this.ˏ = str;
        this.ˊ = z;
    }

    public boolean ˏ() {
        int size = this.ˎ.size();
        List arrayList = new ArrayList();
        for (int i = 0; i < size; i++) {
            String ʽ = ((OriginalMediaBean) this.ˎ.get(i)).ʽ();
            if (!dof.ॱ(ʽ)) {
                String ˋ = ˋ(ʽ);
                if (new File(ʽ).exists()) {
                    String str = this.ˏ + File.separator + (i + 1) + ˋ;
                    this.ˋ.add(str);
                    arrayList.add(new d(ʽ, str, ॱ(ˋ)));
                }
            }
        }
        int size2 = arrayList.size();
        if (size2 == 0) {
            azh.ˏ.ˊ("TranslateImgHelper", "no translate task ");
            return false;
        } else if (size2 == 1) {
            return ((d) arrayList.get(0)).ˏ();
        } else {
            return ˏ(arrayList);
        }
    }

    private String ˋ(String str) {
        if (!this.ˊ) {
            return ".webp";
        }
        String ॱ = bac.ॱ(str);
        if (dof.ˋ(ॱ)) {
            azh.ˏ.ˏ("TranslateImgHelper", "read file type error");
            ॱ = "jpg";
        }
        return "." + ॱ;
    }

    private CompressFormat ॱ(String str) {
        if (".jpg".equals(str)) {
            return CompressFormat.JPEG;
        }
        if (".png".equals(str)) {
            return CompressFormat.PNG;
        }
        if (this.ˊ) {
            return CompressFormat.JPEG;
        }
        return CompressFormat.WEBP;
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private boolean ˏ(java.util.List<java.util.concurrent.Callable<java.lang.Boolean>> r11) {
        /*
        r10 = this;
        r2 = 3;
        r9 = 0;
        r1 = new java.util.concurrent.ThreadPoolExecutor;
        r4 = 10;
        r6 = java.util.concurrent.TimeUnit.SECONDS;
        r7 = new java.util.concurrent.LinkedBlockingQueue;
        r7.<init>();
        r8 = new o.bae$c;
        r0 = 0;
        r8.<init>();
        r3 = r2;
        r1.<init>(r2, r3, r4, r6, r7, r8);
        r2 = 1;
        r0 = r1.invokeAll(r11);	 Catch:{ Exception -> 0x0048 }
        r3 = r0.iterator();	 Catch:{ Exception -> 0x0048 }
    L_0x0020:
        r0 = r3.hasNext();	 Catch:{ Exception -> 0x0048 }
        if (r0 == 0) goto L_0x0073;
    L_0x0026:
        r0 = r3.next();	 Catch:{ Exception -> 0x0048 }
        r0 = (java.util.concurrent.Future) r0;	 Catch:{ Exception -> 0x0048 }
        r0 = r0.get();	 Catch:{ Exception -> 0x0048 }
        r0 = (java.lang.Boolean) r0;	 Catch:{ Exception -> 0x0048 }
        r0 = r0.booleanValue();	 Catch:{ Exception -> 0x0048 }
        if (r0 != 0) goto L_0x0020;
    L_0x0038:
        r0 = r9;
    L_0x0039:
        r1.shutdown();	 Catch:{ Exception -> 0x003d }
    L_0x003c:
        return r0;
    L_0x003d:
        r1 = move-exception;
        r1 = o.azh.ˏ;
        r2 = "TranslateImgHelper";
        r3 = "translate webp shutdown pool error";
        r1.ˏ(r2, r3);
        goto L_0x003c;
    L_0x0048:
        r0 = move-exception;
        r2 = o.azh.ˏ;	 Catch:{ all -> 0x0063 }
        r3 = "TranslateImgHelper";
        r4 = "ExecutorService TranslateImgHelper error:";
        r2.ˊ(r3, r4, r0);	 Catch:{ all -> 0x0063 }
        r1.shutdown();	 Catch:{ Exception -> 0x0057 }
        r0 = r9;
        goto L_0x003c;
    L_0x0057:
        r0 = move-exception;
        r0 = o.azh.ˏ;
        r1 = "TranslateImgHelper";
        r2 = "translate webp shutdown pool error";
        r0.ˏ(r1, r2);
        r0 = r9;
        goto L_0x003c;
    L_0x0063:
        r0 = move-exception;
        r1.shutdown();	 Catch:{ Exception -> 0x0068 }
    L_0x0067:
        throw r0;
    L_0x0068:
        r1 = move-exception;
        r1 = o.azh.ˏ;
        r2 = "TranslateImgHelper";
        r3 = "translate webp shutdown pool error";
        r1.ˏ(r2, r3);
        goto L_0x0067;
    L_0x0073:
        r0 = r2;
        goto L_0x0039;
        */
        throw new UnsupportedOperationException("Method not decompiled: o.bae.ˏ(java.util.List):boolean");
    }

    public List<String> ˊ() {
        return this.ˋ;
    }
}
